var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    tel: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var goods_id = options.id
    var that = this
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    app.ihttpGet("https://www.58zltc.com/api.php/Credit/getGoodsItem/id/" + goods_id, res => {
      that.setData({
        info: res.data,
        goods_id: goods_id
      })
    })
  },
  putName: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  putTel: function(e) {
    this.setData({
      tel: e.detail.value
    })
  },
  backto: function() {
    wx.navigateBack({})
  },
  //兑换
  exchange: function() {
    var that = this
    var info = that.data.info
    //积分兑换
    var post_data = {
      goods_id: that.data.goods_id,
      openid: app.globalData.openid,
      username: that.data.name,
      tel: that.data.tel
    }
    if(that.data.info.shop_id>0){
      var url = 'https://www.58zltc.com/api.php/Credit/exchangeShop';
    }else{
      var url = 'https://www.58zltc.com/api.php/Credit/exchange';
    }
    app.ihttpPost(url, post_data, res => {
      var icon = 'none'
      res.data.error == 0 ? icon = 'success' : ''
      if (res.data.error == 0) {
        wx.showToast({
          title: res.data.msg,
          icon: icon,
          duration: 1500
        })
        setTimeout(function() {
          wx.navigateBack({})
        }, 1500)
      } else if (res.data.error == undefined) {
        var data = res.data;
        var prepay_id = res.data.package;
        var a = prepay_id.slice(10);
        //支付添加formid
        app.ihttpPost("https://www.58zltc.com/api.php/Index/addFormId", {form_id: a, openid: app.globalData.openid, type: 'pay'}, function(res) {})
        wx.requestPayment({
          'appId': data.appId,
          'timeStamp':data.timeStamp,
          'nonceStr': data.nonceStr,
          'package': data.package,
          'signType': 'MD5',
          'paySign': data.paySign,
          'success': res => {
            if (res.errMsg == 'requestPayment:ok') {
              wx.showModal({
                title: '提示',
                content: "支付成功！",
                showCancel: false,
                success: function(res) {
                  wx.navigateBack({
                    
                  })
                }
              })
            }
          }
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: icon,
          duration: 1500
        })
      }
    })
  }

})